<!DOCTYPE html>
<html>
<head>
    <title>目前有效的限时课程/连续购课清单</title>
    <link rel="stylesheet" href="../static/css/minghu.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}" type="image/x-icon">
    <style>
        .title{
            background-color:#289e63;
            font-size: 14px;
            color:#ffffff;
            width:200px;
            border-radius: 4px;
            text-align: left;
            padding-left:8px;
            margin-bottom: 4px;
        }
        .continue-buy-item{
            font-size:12px;
            margin:6px;
        }
        .big-box{
            margin-bottom: 10px;
            padding: 10px;
        }


    </style>
</head>
<body style="background-color: rgb(254, 254, 255);">
    {% include 'show_ins_info.html' %}
    <hr>
    <h2 id="title" style="margin-bottom: 40px;color:#5bcf7e;">目前生效的限时课程<br>及连续购课清单</h2>    
    <div id="msg" style="color:red;padding-top:10px;"></div>
    <div>
        <!-- <hr style="border-top:1px dashed;margin-bottom: 30px;"> -->
        <div id="validContinueBuyList" ></div>
        <div id="validContinueBuyNotOver12List" ></div>
        <div id="invalidContinueBuyList"></div>
    </div>


    <script src="../static/js/common.js"></script>
    <script src="../static/js/echarts.min.js"></script>
    
    <script>
        let insName;
        let insId;
        let insRole;
        let insList;
        
        document.addEventListener('DOMContentLoaded',function(){
            // loginCheck();        
            const insInfo=hideInsSelectBlockAndGetInsInfo();
            insId=insInfo.sessionInsId;
            insName=insInfo.sessionInsName;
            insRole=insInfo.sessionInsRole;
            // console.log(insName)
            //教练角色不允许进入该页面，返回Index
            if(insRole==='ins'){
                window.location.replace('./index');
            }

            const today=new Date();

           
            fetch('/get_ins_list', {
                method: 'GET',
            })
            .then(response => response.json())
            .then(data => {
            //将data赋值给块变量cus_list，供后面的handleInput使用
                console.log('ins list:',data);
                insList=data['ins_list'];
            })
            .catch(error => console.error('Error:', error));
            
            document.getElementById('validContinueBuyList').innerHTML='';
            document.getElementById('invalidContinueBuyList').innerHTML='';
            document.getElementById('validContinueBuyNotOver12List').innerHTML='';
            

            showContinueBuyOver12TakenList();
         

        });

        function showContinueBuyOver12TakenList(){            
            const today=new Date();
            const todayStr=dateToString(today,'date');
            const msg=document.getElementById('msg');
            msg.innerHTML='正在获取清单……';

            fetch('/get_all_continue_buy_cls_rec',{
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body:JSON.stringify({"today":todayStr})
            })
            .then(response=>response.json())
            .then(data=>{
                let continueBuyList=[];
                let notContinueBuyList=[];
                let continueBuyButNotOver12List=[];
                if(data['res']==='ok'){
                    // console.log('continue buy data:',data);
                    msg.innerHTML='';
                    const buyList=data['data'];
                    for(const key in buyList){
                        const item=buyList[key];
                        if(item['continue_buy']==='yes'){
                            if(parseInt(item['cls_tkn_qty'])>=12){
                                if(!continueBuyList.some(rec=>rec['card_id']===item['card_id'])){
                                    continueBuyList.push(item);
                                }
                                
                            }else{
                                if(!continueBuyButNotOver12List.some(rec=>rec['card_id']===item['card_id'])){
                                    continueBuyButNotOver12List.push(item);
                                }
                            
                            }
                            
                        }else{
                            if(!notContinueBuyList.some(rec=>rec['card_id']===item['card_id'])){
                                notContinueBuyList.push(item);
                                }
                        
                        }

                    }

                    // console.log(continueBuyList.length,continueBuyButNotOver12List,notContinueBuyList)
                    showContinueBuyResult(continueBuyList,continueBuyButNotOver12List,notContinueBuyList)

                }else{

                }
            })
            .catch(error=>console.log(error));

            
        }

        function showContinueBuyResult(continueData,continueButNotOver12Data,notContinueData){
            const validContinueBuyList=document.getElementById('validContinueBuyList');
            const invalidContinueBuyList=document.getElementById('invalidContinueBuyList');
            const validContinueBuyNotOver12List=document.getElementById('validContinueBuyNotOver12List');
            validContinueBuyList.innerHTML='';
            invalidContinueBuyList.innerHTML='';
            validContinueBuyNotOver12List.innerHTML='';

            
            if(continueData.length>0){
                let con_num=1;
                const title=document.createElement('div');
                title.classList.add('title');
                title.innerHTML='连续购课3次且上课达12次清单';
                validContinueBuyList.appendChild(title);

                continueData.forEach(element => {
                    const div=document.createElement('div');
                    div.classList.add('continue-buy-item');
                    div.innerHTML=`${con_num}. ${element['cus_name']} ${element['card_id']}，已上${parseInt(element['cls_tkn_qty'])}/${parseInt(element['cls_qty'])}节  @  ${element['ins_names']}`;
                    validContinueBuyList.appendChild(div);
                    
                    con_num+=1;
            
                });
                validContinueBuyList.classList.add('big-box');
            }else{
                validContinueBuyList.innerHTML='';
            }


            
            
            if(continueButNotOver12Data.length>0){
                let con_num=1;
                const title=document.createElement('div');
                title.innerHTML='连续购课3次但上课未达到12次清单';
                title.classList.add('title');
                title.style.backgroundColor='#b59928';
                title.style.width='230px';
                validContinueBuyNotOver12List.appendChild(title);

                continueButNotOver12Data.forEach(element => {
                    const div=document.createElement('div');
                    div.classList.add('continue-buy-item');
                    div.innerHTML=`${con_num}. ${element['cus_name']} ${element['card_id']}，已上${parseInt(element['cls_tkn_qty'])}/${parseInt(element['cls_qty'])}节  @  ${element['ins_names']}`;
                    validContinueBuyNotOver12List.appendChild(div);
                    
                    con_num+=1;
            
                });
                validContinueBuyNotOver12List.classList.add('big-box');
            }else{
                validContinueBuyNotOver12List.innerHTML='';
            }


            if(notContinueData.length>0){
                let con_num=1;
                const title=document.createElement('div');
                title.innerHTML='未连续购课3次的限时课程清单';
                title.classList.add('title');
                title.style.backgroundColor='#2878b5';
                title.style.width='200px';
                
                invalidContinueBuyList.appendChild(title);

                notContinueData.forEach(element => {
                    const div=document.createElement('div');
                    div.classList.add('continue-buy-item');
                    div.innerHTML=`${con_num}. ${element['cus_name']} ${element['card_id']}，已上${parseInt(element['cls_tkn_qty'])}/${parseInt(element['cls_qty'])}节  @  ${element['ins_names']}`;
                    invalidContinueBuyList.appendChild(div);
                    
                    con_num+=1;
            
                });
                invalidContinueBuyList.classList.add('big-box');
            }else{
                invalidContinueBuyList.innerHTML='';
            }

        }


      


    </script>
  
</body>
</html>
